// pages/campMap/campMap.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    location: {
      longitude: "",
      latitude: "",
      name: "",
      address: ""
    },
    markers:[]
  },
  nReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap')
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function (res) {
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker: function () {
    this.mapCtx.translateMarker({
      markerId: 1,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude: 23.10229,
        longitude: 113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function () {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude: 23.10229,
        longitude: 113.3345211,
      }, {
        latitude: 23.00229,
        longitude: 113.3345211,
      }]
    })
  },
  addMarker() {
    // 获取新的标注点信息
    const newMarker = {
      id: this.data.markers.length + 1,
      latitude: 31.148330,
      longitude: 121.529420,
      title: '新标注点',
      iconPath: '/images/new_marker.png',
      width: 30,
      height: 30
    };
    // 更新标注点数组
    const updatedMarkers = [...this.data.markers, newMarker];
    this.setData({
      markers: updatedMarkers
    });
  },
  onLoad: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        });
      },
      fail: function (err) {
        console.error("获取位置信息失败", err);
      }
    });
    wx.onLocationChange(function (res) {
      clearTimeout(locationUpdateTimer);
      locationUpdateTimer = setTimeout(() => {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        });
      }, 1000); // 每隔1秒更新一次位置，可根据实际情况调整
    });
  },
  
  chooseMapViewTap: function () {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        // success
        console.log(res, "location")
        that.setData({
          location: {
            longitude: res.longitude,
            latitude: res.latitude,
            address: res.address,
            name: res.name
          },
          detail_info: res.address,
          wd: res.latitude,
          jd: res.longitude
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad(options) {

  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})